package cn.stylefeng.guns.sys.modular.system.controller;


import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyRbsc;
import cn.stylefeng.guns.sys.modular.system.service.BtyRbscService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/btyRbsc")
public class BtyRbscController {

    private String PREFIX = "/modular/system/btyRbsc/";

    @Resource
    private BtyRbscService btyRbscService;

    /**
     * 跳转到日报审查页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyRbsc.html";
    }

    /**
     * 跳转到识别信息页面
     */
    @RequestMapping("/discern")
    public String discern() {
        return PREFIX + "discern.html";
    }


    /**
     * 获取日报列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public List<BtyRbsc> list(BtyRbsc btyRbsc) {
        /*//获取用户的帐号
        Long userId= LoginContextHolder.getContext().getUserId();
        String username=LoginContextHolder.getContext().getUser().getName();*/
        return btyRbscService.list(btyRbsc);
    }

    /**
     * 获取最新日期
     */
    @RequestMapping(value = "/selectDate")
    @ResponseBody
    public String selectDate() {
        return btyRbscService.selectDate();
    }


    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUser(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        Boolean flag;
        JsonResult jsonResult = new JsonResult();
        try {
            flag = btyRbscService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return flag;
    }

    /**
     * 识别信息
     */
    /*@RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String authHost = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=8r3Y59AXq4XfHq0sE2I6zNfa&client_secret=AC4rpmgYAlp5a9kh0Lm0MRDqKCp897VF";

        try {
            flag = btyRbscService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        // 获取token地址
        String result = "";
        RestTemplate restTemplate = new RestTemplate();
        String connection = restTemplate.getForObject(authHost, String.class);
        URL realUrl = new URL(authHost);
            // 打开和URL之间的连接
        HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
        // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                //logger.info(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        return 1;
    }*/


    /**
     * 上传图片
     */
    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public Map<String, Object> uploads(@RequestPart("file") MultipartFile file, String packName) {
        String authHost = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=8r3Y59AXq4XfHq0sE2I6zNfa&client_secret=AC4rpmgYAlp5a9kh0Lm0MRDqKCp897VF";
        RestTemplate restTemplate = new RestTemplate();
        Map<String, Object> msg = new HashMap<>(16);
        msg.put("token", restTemplate.getForObject(authHost, String.class));
        return msg;
    }
}